﻿using System;
using System.Collections.Generic;
using MDK;


namespace ProtonAnnihilation
    {
    internal class MesonOperator
    {
        // Fields
        private double E;
        private double iE;

        double P;
        double iP;

        private Particle[] Mesons;
        private Random rnd;
        private Particle X;

        // Methods
        private MesonOperator()
        {
            this.Mesons = new Particle[4];
            this.X = new Particle();
            this.rnd = new Random();
            this.E = 558.0;
        }

        public MesonOperator(double InitialEnergy)
        {
            this.Mesons = new Particle[4]; // видимые частицы
            this.X = new Particle(); // X - компаунд-частица(omgega0)
            this.rnd = new Random();  // генератор случайных чисел
            
            this.E = 558.0; // энергия, затраченная на рождение мезонов
            this.iE = this.rnd.Next(558, (int) InitialEnergy); // энергия невидимок
            this.E += InitialEnergy - this.iE;

            this.P = 0;
            this.iP = this.rnd.Next(0, 1680);
            this.P += 1680 - iP;

            ArrangeEnergy();
        }

        private void ArrangeEnergy()
        {
            //части, на которые мы делим всю энергию
            double[] parts = new double[5];

            parts[0] = 0.0;
            parts[1] = this.rnd.NextDouble();
            parts[2] = this.rnd.NextDouble();
            parts[3] = this.rnd.NextDouble();
            parts[4] = 1.0;

            Array.Sort(parts);

            for (int i = 0; i < this.Mesons.Length; i++)
            {
                this.Mesons[i] = new Particle(139.57);

                this.Mesons[i].m = 139.57;
                this.Mesons[i].E += this.E * (parts[i + 1] - parts[i]);
                this.Mesons[i].p = Math.Sqrt((this.Mesons[i].E * this.Mesons[i].E) - (this.Mesons[i].m * this.Mesons[i].m));
            }
        }

        public bool CheckNeutralPion()
        {
            double[] EnergiesAndImpulses = this.MakeSum();

            this.X.E = this.rnd.Next(0, (int) this.iE);
            //this.X.Mass = Math.Sqrt(((2290.0 - EnergiesAndImpulses[0]) * (2290.0 - EnergiesAndImpulses[0])) - ((1610.0 - EnergiesAndImpulses[1]) * (1610.0 - EnergiesAndImpulses[1])));
            
            this.X.Mass = Math.Sqrt(X.E*X.E - Math.Pow( rnd.Next(0, (int)Math.Abs(1610.0 - EnergiesAndImpulses[1])) , 2) );
            this.X.p = Math.Sqrt((this.X.E * this.X.E) - (this.X.m * this.X.m));
            
            return (((int) this.X.m) > 135);
        }

        private double[] MakeSum()
        {
            double E = 0.0;
            double P = 0.0;

            foreach (Particle particle in this.Mesons)
            {
                E += particle.E;
                P += particle.p;
            }
            return new double[] { E, P };
        }

        public double[] TakeSetsEnergies()
        {
            this.X.Mass = 135.0;
            List<double> list = new List<double>();

            foreach (Particle[] particleArray2 in new Particle[][] { 
                                                        new Particle[] { this.Mesons[0], this.Mesons[1], this.X }, 
                                                        new Particle[] { this.Mesons[0], this.Mesons[3], this.X }, 
                                                        new Particle[] { this.Mesons[2], this.Mesons[1], this.X }, 
                                                        new Particle[] { this.Mesons[2], this.Mesons[3], this.X } })
            {
                Particle particle = new Particle();
                particle.CollapsedFrom(particleArray2);

                if ((particle.Mass > 415.0) && (particle.Mass < 2010.0))
                {
                    list.Add(particle.Mass);
                }
            }
            return list.ToArray();
        }

        public override string ToString()
        {
            object obj2;
            string str = "";
            foreach (Particle particle in this.Mesons)
            {
                obj2 = str;
                str = string.Concat(new object[] { obj2, "\r\n", particle.Mass, " ", particle.E, " ", particle.p });
            }
            obj2 = (str + "\r\n iE: " + this.iE) + "\r\n E: " + this.E;
            return string.Concat(new object[] { obj2, "\r\n - ", this.X.m, this.X.E, this.X.p });
        }
    }

}

 
